import 'package:flutter/widgets.dart';
import 'package:library_base/entities/goods_child_info_entity.dart';
import 'package:library_base/ext/double_ext.dart';
import 'package:library_base/ext/list_ext.dart';
import 'package:library_base/ext/string_ext.dart';
import 'package:library_base/provider/base_controller.dart';
import 'package:get/get.dart';
import 'package:library_base/utils/get_util.dart';
import 'package:library_base/utils/num_util.dart';

class SingleHeadEntryController extends BaseController {
  ///底部显示文本的总重量
  Rx<double> totalWeight = 0.0.obs;

  ///总的服务费用
  double totalServiceCharge = 0.0;

  ///判断是否可以点击保存
  Rx<bool> isCanSave = false.obs;

  /// 临时的数据列表,如不确认直接返回不影响原数据
  RxList<GoodsChildInfoEntity> goodsChildInfo = RxList<GoodsChildInfoEntity>();

  Rx<int> itemCount = 5.obs;

  bool isEdit = false;

  ///前缀
  late String prefix;

  late String unit;

  List<TextEditingController> barcodeControllers = [];

  List<TextEditingController> weightControllers = [];

  List<TextEditingController> serviceChargeControllers = [];

  void Function(
    List<GoodsChildInfoEntity>,
  )? callbackGoodsChildInfoEntity;

  @override
  void onCreate() {
    var argumentMap = getArguments();
    if (argumentMap != null) {
      goodsChildInfo.value = argumentMap['GoodsChildInfoEntity'];
      itemCount.value = argumentMap['ItemCount'];
      unit = argumentMap['unit'];
      callbackGoodsChildInfoEntity = argumentMap['callbackGoodsChildInfoEntity'];
      isEdit = goodsChildInfo.value.isNotEmpty;
    }

    if (isEdit) {
      prefix = goodsChildInfo.value[0].goodsPurchaseCode.substring(0,11);
      for (var element in goodsChildInfo.value) {
        barcodeControllers.add(TextEditingController(text: element.goodsPurchaseCode.substring(11,element.goodsPurchaseCode.length)));
        weightControllers.add(TextEditingController(text: element.weight.remoteUnitTransitionLocalUnitStr(unit)));
        serviceChargeControllers.add(TextEditingController(text: element.serviceCost.toString()));
      }
    } else {
      DateTime dateTime= DateTime.now();
      var day = dateTime.toString().substring(8,10);
      var month = dateTime.toString().substring(5,7);
      var year = dateTime.toString().substring(2,4);
      prefix = "Z7035$year$month$day";
      for (int i = 0; i < itemCount.value; i++) {
        barcodeControllers.add(TextEditingController());
        weightControllers.add(TextEditingController());
        serviceChargeControllers.add(TextEditingController());
      }
    }

    isCanSave.value = isCanClickDetermine();
    totalWeight.value = calculateTotalWeight();
    totalServiceCharge = calculateTotalServiceCharge();
  }

  ///判断是否可以保存
  bool isCanClickDetermine() {
    for (int i = 0; i < barcodeControllers.length; i++) {
      var barcodeController = barcodeControllers[i];
      var weightController = weightControllers[i];
      var serviceChargeController = serviceChargeControllers[i];

      if (barcodeController.text.isEmpty) {
        return false;
      }

      if (!NumUtil.isNumber(weightController.text)) {
        return false;
      } else {
        double weight = double.parse(weightController.text);
        if (weight == 0.0) {
          return false;
        }
      }

      if (NumUtil.isNumber(serviceChargeController.text)) {
        double serviceCharge = double.parse(serviceChargeController.text);
        if (serviceCharge < 0.0) {
          return false;
        }
      }
    }
    return true;
  }

  ///计算总重量
  double calculateTotalWeight() {
    double temporaryTotalWeight = 0.0;
    for (int i = 0; i < weightControllers.length; i++) {
      var weightController = weightControllers[i];
      if (NumUtil.isNumber(weightController.text)) {
        temporaryTotalWeight = NumUtil.add(temporaryTotalWeight, weightController.text.strAsUnitNum());
      }
    }
    return temporaryTotalWeight.remoteUnitTransitionLocalUnit(unit);
  }

  ///计算总的服务费
  double calculateTotalServiceCharge() {
    double temporaryTotalServiceCharge = 0.0;
    for (int i = 0; i < serviceChargeControllers.length; i++) {
      var serviceChargeController = serviceChargeControllers[i];
      if (NumUtil.isNumber(serviceChargeController.text)) {
        temporaryTotalServiceCharge =
            NumUtil.add(temporaryTotalServiceCharge, double.parse(serviceChargeController.text));
      }
    }
    return temporaryTotalServiceCharge;
  }

  ///条码变更
  void barcodeChange(String value) {
    isCanSave.value = isCanClickDetermine();
    update();
  }

  ///总重量
  void weightChange(String value) {
    double temporaryTotalWeight = calculateTotalWeight();
    if (temporaryTotalWeight != totalWeight.value.numAsUnit()) {
      totalWeight.value = temporaryTotalWeight;
    }
    isCanSave.value = isCanClickDetermine();
    update();
  }

  ///总服务费变动
  void serviceChargeChange(String value) {
    double temporaryServiceCharge = calculateTotalServiceCharge();
    if (temporaryServiceCharge != totalServiceCharge) {
      totalServiceCharge = temporaryServiceCharge;
    }
    isCanSave.value = isCanClickDetermine();
    update();
  }

  ///总服务费变动
  void determine() {
    List<GoodsChildInfoEntity> lastSaveGoodsChildInfo = <GoodsChildInfoEntity>[];

    for (int i = 0; i < barcodeControllers.length; i++) {
      var barcodeController = barcodeControllers[i];
      var weightController = weightControllers[i];
      var serviceChargeController = serviceChargeControllers[i];

      var goodsChildInfoEntity = GoodsChildInfoEntity();
      goodsChildInfoEntity.goodsPurchaseCode = prefix+barcodeController.text;
      goodsChildInfoEntity.weight = weightController.text.strAsUnitNum();
      goodsChildInfoEntity.serviceCost = NumUtil.isNumber(serviceChargeController.text) ? double.parse(serviceChargeController.text):0.0;
      lastSaveGoodsChildInfo.add(goodsChildInfoEntity);
    }
    callbackGoodsChildInfoEntity?.call(lastSaveGoodsChildInfo);
    GetHelper.back();
  }

  bool onBackAction() {
    if (isEdit) {
      if (!isCanClickDetermine()) {
        List<GoodsChildInfoEntity> lastSaveGoodsChildInfo = <GoodsChildInfoEntity>[];

        for (int i = 0; i < barcodeControllers.length; i++) {
          var barcodeController = barcodeControllers[i];
          var weightController = weightControllers[i];
          var serviceChargeController = serviceChargeControllers[i];

          //   if (barcodeController.text.isEmpty ||
          //       weightController.text.isEmpty ||
          //       serviceChargeController.text.isEmpty)
          //     var goodsChildInfoEntity = GoodsChildInfoEntity();
          //   goodsChildInfoEntity.goodsPurchaseCode = barcodeController.text;
          //   goodsChildInfoEntity.weight = double.parse(weightController.text);
          //   goodsChildInfoEntity.serviceCost = double.parse(serviceChargeController.text);
          //   lastSaveGoodsChildInfo.add(goodsChildInfoEntity);
        }

        //未填写完毕

        return false;
      } else {
        List<GoodsChildInfoEntity> lastSaveGoodsChildInfo = <GoodsChildInfoEntity>[];

        for (int i = 0; i < barcodeControllers.length; i++) {
          var barcodeController = barcodeControllers[i];
          var weightController = weightControllers[i];
          var serviceChargeController = serviceChargeControllers[i];

          var goodsChildInfoEntity = GoodsChildInfoEntity();
          goodsChildInfoEntity.goodsPurchaseCode = barcodeController.text;
          goodsChildInfoEntity.weight = double.parse(weightController.text);
          goodsChildInfoEntity.serviceCost = double.parse(NumUtil.isNumber(serviceChargeController.text) ? serviceChargeController.text:"0.0");
          lastSaveGoodsChildInfo.add(goodsChildInfoEntity);
        }
        if (lastSaveGoodsChildInfo.equal(goodsChildInfo.value)) {
          //数据不变
          return false;
        } else {
          //数据有变
          return true;
        }
      }
    } else {
      for (int i = 0; i < itemCount.value; i++) {
        if (barcodeControllers[i].text.isNotEmpty ||
            weightControllers[i].text.isNotEmpty ||
            serviceChargeControllers[i].text.isNotEmpty) {
          //数据有变动
          return true;
        }
      }
      return false;
    }
  }
}
